package org.openkoala.koala.deploy.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.openkoala.koala.deploy.action.WarEJBDeployAction;
import org.openkoala.koala.deploy.ejb.pojo.EJBDeployConfig;

/**
 * EJB配置的dialog
 * @author lingen
 *
 */
public class EJBConfigDialog {
	
	private Shell shell;
	
	private Text ipText;
	
	private Text portText;
	
	private Button ok;
	
	private Button cancel;
	
	private Combo servers;
	
	private WarEJBDeployAction warEjbAction;

	public EJBConfigDialog(Shell shell,WarEJBDeployAction warEjbAction) {
		this.shell = shell;
		this.warEjbAction = warEjbAction;
		drawUI(shell);
	}
	
	private void drawUI(final Shell shell){
		shell.setText("ejb deploy config");
		shell.setSize(480, 300);
		shell.setLayout(new FormLayout());
		
		Label ipLabel = new Label(shell, SWT.NONE);
		ipLabel.setText("IP:");
		FormData data = new FormData();
		data.left = new FormAttachment(0,10);
		data.top = new FormAttachment(0,10);
	    data.right = new FormAttachment(ipLabel,40,SWT.LEFT);
		data.bottom = new FormAttachment(ipLabel,30,SWT.TOP);
		ipLabel.setLayoutData(data);
		
		ipText = new Text(shell,SWT.BORDER);
		ipText.setText("127.0.0.1");
		data = new FormData();
		data.left = new FormAttachment(ipLabel,60);
		data.top = new FormAttachment(0,10);
		data.right = new FormAttachment(ipText,120,SWT.LEFT);
		data.bottom = new FormAttachment(ipText,30,SWT.TOP);
		ipText.setLayoutData(data);
		
		Label portLabel = new Label(shell, SWT.NONE);
		portLabel.setText("Port");
		data = new FormData();
		data.left = new FormAttachment(0,10);
		data.top = new FormAttachment(ipLabel,20);
	    data.right = new FormAttachment(portLabel,40,SWT.LEFT);
		data.bottom = new FormAttachment(portLabel,30,SWT.TOP);
		portLabel.setLayoutData(data);
		
		portText = new Text(shell,SWT.BORDER);
		portText.setText("1099");
		data = new FormData();
		data.left = new FormAttachment(portLabel,60);
		data.top = new FormAttachment(ipText,20);
		data.right = new FormAttachment(portText,120,SWT.LEFT);
		data.bottom = new FormAttachment(portText,30,SWT.TOP);
		portText.setLayoutData(data);
		
		Label serverLabel = new Label(shell, SWT.NONE);
		serverLabel.setText("Server");
		data = new FormData();
		data.left = new FormAttachment(0,10);
		data.top = new FormAttachment(portLabel,20);
	    data.right = new FormAttachment(serverLabel,40,SWT.LEFT);
		data.bottom = new FormAttachment(serverLabel,30,SWT.TOP);
		serverLabel.setLayoutData(data);
		
		servers = new Combo(shell,SWT.DROP_DOWN);
		servers.add("JBoss-EAP-4");
		servers.add("JBoss-EAP-6");
		servers.add("JBoss-AS-7");
		servers.select(0);
		data = new FormData();
		data.left = new FormAttachment(serverLabel,60);
		data.top = new FormAttachment(portLabel,20);
		data.right = new FormAttachment(servers,120,SWT.LEFT);
		data.bottom = new FormAttachment(servers,30,SWT.TOP);
		servers.setLayoutData(data);
		servers.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				String selected = servers.getText();
				if("JBoss-EAP-4".equals(selected)){
					portText.setText("1099");
				}
				if("JBoss-AS-7".equals(selected) || "JBoss-EAP-6".equals(selected)){
					portText.setText("4447");
				}
			}});
		
		
		ok = new Button(shell,SWT.PUSH);
		ok.setText("OK");
		data = new FormData();
		data.left = new FormAttachment(100,-250);
		data.top = new FormAttachment(100,-60);
		data.right = new FormAttachment(ok,80,SWT.LEFT);
		data.bottom = new FormAttachment(ok,30,SWT.TOP);
		ok.setLayoutData(data);
		
		ok.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
				EJBDeployConfig config = EJBDeployConfig.getDefaultEJBDeployConfig();
				config.setIp(ipText.getText());
				config.setPort(portText.getText());
				config.setServer(servers.getText());
				shell.close();
				warEjbAction.process(config);
			}});
		
		cancel = new Button(shell,SWT.PUSH);
		cancel.setText("Cancel");
		data = new FormData();
		data.left = new FormAttachment(ok,20);
		data.top = new FormAttachment(100,-60);
		data.right = new FormAttachment(cancel,80,SWT.LEFT);
		data.bottom = new FormAttachment(cancel,30,SWT.TOP);
		cancel.setLayoutData(data);
		
		cancel.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {	
			}
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shell.close();
			}});
		this.shell.open();
	}
	
}
